<template>
  <div class="newProject">
    <div style="width: 95%;height: 838px; margin: auto;margin-top: 20px;">
      <div style="width: 100%;height: 46px;align-items: center;border-bottom: 2px solid #DCDFE6;margin-bottom: 30px; display: flex;
  align-items: center;  ">
        <span
          style="font-family: PingFangSC-Medium;font-weight: 600;font-size: 16px;color: #303133;line-height: 24px;">新建项目</span>
      </div>
      <div>
        <el-form ref="form" :model="form" :rules="rules">
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目编号：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="no">
                <el-input v-model="form.no" placeholder="请输入项目编号" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span>技术合同登记号：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <el-input style="height: 28px;" placeholder="请输入技术合同登记号" v-model="form.contractNo"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目名称：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="name">
                <el-input v-model="form.name" placeholder="请输入项目名称" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">立项单位：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="establishUnit">
                <el-input v-model="form.establishUnit" placeholder="请输入立项单位"
                  style="height: 28px !important;"></el-input>
                <!-- <el-select v-model="form.establishUnit" placeholder="请选择立项单位" style="width: 100%; ">
                  <el-option v-for="item in unitOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select> -->
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">项目类型：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="type">
                <el-select v-model="form.type" placeholder="请选择项目类型" style="width: 100%; ">
                  <el-option v-for="item in projectOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目开始时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="establishTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.establishTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style=" width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">项目完成时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="endTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.endTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style=" width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目总经费(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="totalFee">
                <el-input v-model="form.totalFee" placeholder="请输入项目总经费" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span>项目到款额(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <el-input style="height: 28px;" placeholder="请输入项目到款额" v-model="form.receivedMoney"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">牵头部门：</span>
            </el-col>
            <el-col :span="6" class="colRight" prop="leadDeptId">
              <el-select @change="changeSel" v-model="form.leadDeptId" placeholder="请选择牵头部门" style="width: 100%; ">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              <!-- <el-cascader :options="options" :show-all-levels="false" v-model="form.leadDeptId" placeholder="请选择牵头部门"
                :props="{ checkStrictly: true }" style="width: 100%;" clearable></el-cascader> -->
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">项目下达性质：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="nature">
                <el-select v-model="form.nature" placeholder="请选择任务下达性质" style="width: 100%; ">
                  <el-option v-for="item in natureOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>项目描述：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入项目描述" v-model="form.description">
              </el-input>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>备注：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入备注" v-model="form.remark">
              </el-input>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px;">
            <el-button type="primary" @click="onSubmit" style="background-color: #47ACF4;">确定</el-button>
            <el-button type="primary" @click="cancel"
              style="margin-left: 30px;background: transparent; color: #47ACF4;margin-right:24px ;">取消</el-button>
            <!-- <el-button>取消</el-button> -->
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { getDeptList, addProject } from "@/api/project/project";
export default {
  components: {
  },
  data() {
    var validateData = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入完成时间'));
      } else if (this.form.establishTime > value) {
        callback(new Error('完成时间必须大于开始时间'));
      } else {
        callback();
      }
    };
    var startData = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入开始时间'));
      } else if (value > this.form.endTime && this.form.endTime) {
        callback(new Error('开始时间必须小于完成时间'));
      } else {
        callback();
      }
    };
    return {
      form: {
        endTime: '',
        contractNo: '',
        establishTime: '',
        establishUnit: '',
        delFlag: 0,
        id: '',
        leadDeptId: '',
        name: '',
        nature: '',
        no: '',
        receivedMoney: '',
        status: '',
        totalFee: '',
        type: '',
        description: '',
        remark: ''
      },
      unitOptions: [],
      projectOptions: [],
      natureOptions: [],
      options: [],
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
        ],
        establishTime: [
          { required: true, message: '请输入项目开始时间', trigger: 'blur' },
          { validator: startData, trigger: 'blur' },
        ],
        endTime: [
          { required: true, message: '请输入项目完成时间', trigger: 'blur' },
          { validator: validateData, trigger: 'blur' },
        ],
        establishUnit: [
          { required: true, message: '请输入立项单位', trigger: 'blur' },
        ],
        leadDeptId: [
          { required: true, message: '请输入牵头部门', trigger: 'blur' },
        ],
        no: [
          { required: true, message: '请输入项目编号', trigger: 'blur' },
        ],
        totalFee: [
          { required: true, message: '请输入项目总经费', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请输入项目类型', trigger: 'blur' },
        ],
        nature: [
          { required: true, message: '请输入项目下达性质', trigger: 'blur' },
        ],
      }
    }
  },
  mounted() {
    getDeptList().then(res => {
      this.options = res.data[0].children
      console.log("dept", this.options);
    });
    this.natureOptions = this.getDict('xmgl_project_nature');
    this.unitOptions = this.getDict('xmgl_project_initiationunit');
    this.projectOptions = this.getDict('xmgl_project_type');
  },
  methods: {
    onSubmit() {
      if (this.form.establishTime > this.form.endTime) {
        alert("开始时间必须小于完成时间")
        return
      }

      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.leadDeptId = this.form.leadDeptId[this.form.leadDeptId.length - 1]
          addProject(this.form).then(res => {
            if (res.code = 200) {
              this.$modal.msgSuccess("新建项目成功！")
              this.$router.push({
                path: '/projectList',
              });
            }
          });
          console.log('submit!', this.form);
        } else {
          this.$modal.msgError('请填写完完整信息！');
        }
      })
    },
    cancel() {
      this.$router.go(-1);
    },
    getDict(dictName) {
      let dict = this.$store.state.dictNew[dictName]
      return dict
      // console.log('task', dict)
    }
  }
}
</script>

<style scoped>
.newProject {
  width: 95%;
  height: 100%;
  background-color: #FFFFFF;
  margin-top: 20px;

  .colRight {

    padding: 10px;
    background-color: #FFFFFF;
  }

  .colLeft {
    /* border: 1px solid #DCDFE6; */
    background-color: #F5F7FA;
    /* text-align: center; */
    margin: auto;
  }
}

span.required::after {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
<style>
.el-form-item__error {
  z-index: 999 !important;
}
</style>
